/*
 * @(#)MemcachedSessionFilter.java 1.0.0 12/11/16
 * Copyright 2012© Emagsoftware Technology Co., Ltd. All Rights reserved.
 */

package com.love.web.session.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StandardSessionFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
		SessionContext.filterConfig = filterConfig;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		if(SessionContext.maxInactiveInterval == null){
			SessionContext.maxInactiveInterval = httpServletRequest.getSession().getMaxInactiveInterval();
		}
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		Store store = new RedisStore(null);
		Manager manager = new StandardManager(httpServletRequest, httpServletResponse, store);
		MyHttpServletRequest myHttpServletRequest = new MyHttpServletRequest(httpServletRequest, httpServletResponse, manager);
		try {
			chain.doFilter(myHttpServletRequest, response);
		} finally {
			myHttpServletRequest.endAccess();
		}
	}

	public void destroy() {
		
	}

}
